﻿using BP.Difference;
using BP.Sys;
using BP.Web;
using System;
using System.Collections;

namespace BP.DA
{
    public class SQLFlow
    {
        public SQLFlow() { }

        public static string Gener_Flow(string mark, Hashtable ht = null)
        {
            string key = null;
            if (ht != null && ht.ContainsKey("Key") == true)
                key = ht["Key"].ToString();

            //流程类别
            if (mark.Equals("Flow_FlowSorts") == true)
            {
                if (SystemConfig.CCBPMRunModel == CCBPMRunModel.Single)
                    return "SELECT No,Name,ParentNo FROM WF_FlowSort Order by NO,Idx  ";
                else
                    return "SELECT No,Name,ParentNo FROM WF_FlowSort WHERE OrgNo= '" + WebUser.OrgNo + "' Order By NO,Idx ";
            }
            //流程列表
            if (mark.Equals("Flow_Flows") == true)
            {
                if (SystemConfig.CCBPMRunModel == CCBPMRunModel.Single)
                    return "SELECT No,Name,FK_FlowSort GroupNo FROM WF_Flow Order By Idx ";
                else
                    return "SELECT No,Name,FK_FlowSort GroupNo FROM WF_Flow WHERE OrgNo= '" + WebUser.OrgNo + "' Order By Idx ";
            }
            //节点列表
            if (mark.Equals("Flow_Nodes") == true)
            {
                return "SELECT NodeID as No,Name FROM WF_Node WHERE FK_Flow='" + ht["FK_Flow"] + "' Order by Step ";
            }
            if (mark.Equals("Flow_NodesByKey") == true)
            {
                return "SELECT NodeID as No,Name FROM WF_Node WHERE FK_Flow='" + key + "' Order by Step ";
            }
            //单流程管理员大屏类的sql.
            if (mark.StartsWith("Flow_DataV_OneFlowAdmin") == true)
                return DataV_OneFlowAdmin(mark, ht);
            //用户大屏类的sql.
            if (mark.StartsWith("Flow_DataV_OneFlowEmp") == true)
                return DataV_OneFlowEmp(mark, ht);
            //新建子流程 GPN_NewSubFlow  新建子流程时排除父流程
            if (mark.Equals("Flow_SubFlowSrcList") == true)
            {
               return "SELECT No,Name,FK_FlowSort as GroupNo FROM WF_Flow WHERE No != '" + key + "' ORDER BY Idx ";
            }
            //工作批量移交 GPN_WorkShiftBatch  请选择规则
            if (mark.Equals("Flow_WorkShiftBatchFlows") == true)
            {
                return "SELECT WorkID AS No,Title AS Name FROM wf_generworkflow WHERE TodoEmps LIKE '%" + key + "%' AND (WFState=2 OR WFState=5)";
            }

            //Org.cs beforeUpdateInsertAction方法使用
            if (mark.Equals("Flow_FlowNums") == true)
            {
                return "SELECT COUNT(*) AS a FROM WF_Flow WHERE OrgNo='" + WebUser.OrgNo + "'";
            }
            if (mark.Equals("Flow_GWFS") == true)
            {
                return "SELECT COUNT(*) AS a FROM WF_GenerWorkFlow WHERE OrgNo='" + WebUser.OrgNo + "' AND WFState!=3";
            }
            if (mark.Equals("Flow_GWFSOver") == true)
            {
                return "SELECT COUNT(*) AS a FROM WF_GenerWorkFlow WHERE OrgNo='" + WebUser.OrgNo + "' AND WFState=3";
            }
            return null;
        }

        //单流程管理员大屏类的sql.
        public static string DataV_OneFlowAdmin(string mark, Hashtable ht = null)
        {
            string key = null;
            if (ht != null && ht.ContainsKey("Key") == true)
                key = ht["Key"].ToString();

            switch (mark)
            {
                case "Flow_DataV_OneFlowAdmin_Todo": //待办.
                    return "SELECT count(*) as Num FROM WF_GenerWorkerList WHERE IsPass=0 AND FK_Flow=" + key;
                case "Flow_DataV_OneFlowAdmin_Return": //退回.
                    return "SELECT count(*) as Num FROM WF_GenerWorkFlow WHERE WFState=5 AND FK_Flow=" + key;
                case "Flow_DataV_OneFlowAdmin_Runing": //未完成.
                    return "SELECT count(*) as Num FROM WF_GenerWorkFlow WHERE  WFState In (2,4,5,6,7,8,9,10,11) AND FK_Flow=" + key;
                case "Flow_DataV_OneFlowAdmin_Complete": //已完成.
                    return "SELECT count(*) as Num FROM WF_GenerWorkFlow WHERE  WFState=3 AND FK_Flow=" + key;
                case "Flow_DataV_OneFlowAdmin_TodoFlow": //待办流程发布.
                    return "SELECT NodeName, Count(*) as Num FROM WF_GenerWorkerList WHERE IsPass=0 AND FK_Flow='" + key + "'group by NodeName";
                case "Flow_DataV_OneFlowAdmin_Average": //节点平均工作时长(H).
                    return "SELECT NodeIDT, Count(*) as Num FROM WF_CH WHERE FlowNo='" + key + "' Group by NodeIDT ";
                default:
                    break;
            }
            throw new Exception("err@没有判断的SQL标记:" + mark);
        }
        //用户大屏类的sql.
        public static string DataV_OneFlowEmp(string mark, Hashtable ht = null)
        {
            string key = null;
            if (ht != null && ht.ContainsKey("Key") == true)
                key = ht["Key"].ToString();

            switch (mark)
            {
                case "Flow_DataV_OneFlowEmp_Todo": //待办.
                    return "SELECT count(*) as Num FROM WF_GenerWorkFlow WHERE TodoEmps LIKE '%" + WebUser.No + "%' AND WFState>=1 AND FK_Flow='" + key +"'";
                case "Flow_DataV_OneFlowEmp_Return": //退回.
                    return "SELECT count(*) as Num FROM WF_GenerWorkFlow WHERE TodoEmps LIKE  '%" + WebUser.No + "%' AND WFState=5 AND FK_Flow='" + key + "'";
                case "Flow_DataV_OneFlowEmp_Runing": //未完成.
                    return "SELECT count(*) as Num FROM WF_GenerWorkFlow WHERE TodoEmps LIKE  '%" + WebUser.No + "%' AND WFState=5 AND FK_Flow='" + key + "'";
                case "Flow_DataV_OneFlowEmp_Complete": //已完成.
                    return "SELECT count(*) as Num FROM WF_GenerWorkFlow WHERE TodoEmps LIKE '%" + WebUser.No + "%' AND WFState=3 AND FK_Flow='" + key + "'";
                case "Flow_DataV_OneFlowEmp_TodoFlow": //待办流程发布.
                    return "SELECT NodeName, Count(*) as Num FROM WF_GenerWorkFlow WHERE TodoEmps LIKE  '%" + WebUser.No + ",%' AND FK_Flow='" + key + "' Group BY NodeName";
                default:
                    break;
            }
            throw new Exception("err@没有判断的SQL标记:" + mark);
        }
    }
    
}
